﻿package birdry.ui	{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	/**
	* ...
	* @author 
	*/
	public class Scratch	extends EventDispatcher	{
		
		public static const COMPLETE:String = "complete";
		
		var image:DisplayObject;
		var hit:SimpleButton;
		var size:uint;
		var alpha:Number;
		var debugFlag:Boolean;
		var container:DisplayObjectContainer;
		
		var maskSpr:Sprite;
		var checkSpr:Sprite;
		var debugSpr:Sprite;
		var bd:BitmapData;
		var bm:Bitmap;
		
		public function Scratch( image:DisplayObject, hit:SimpleButton, debugFlag:Boolean = false, size:uint = 20, alpha:Number = 1): void	{
			this.image = image;
			this.hit = hit;
			this.size = size;
			this.alpha = alpha;
			this.debugFlag = debugFlag;
			this.container = image.parent;
			
			setLayout();
		}
		
		private function setLayout():void		{
			
			checkSpr = new Sprite();
			maskSpr = new Sprite();
			debugSpr = new Sprite();
			
			
			maskSpr.cacheAsBitmap = true;
			image.cacheAsBitmap = true;
			image.mask = maskSpr;
			
			container.addChild(image);
			container.addChild(maskSpr);
			container.addChild(hit);
			reset();
		}
		
		public function reset(): void	{
			maskSpr.graphics.clear();
			checkSpr.graphics.clear();
			debugDraw(20, 30, 220, 130);
			if (debugFlag)	container.addChild(debugSpr);
			createBitmap();
		}
		
		public function play(): void	{
			hit.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		
		public function stop(): void	{
			hit.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
			image.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			image.stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		public function scratch(): void	{
			scratching(container.mouseX, container.mouseY);
			image.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			image.stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		private function onDown(e:MouseEvent):void 		{
			scratch();
		}
		
		private function onMove( e: MouseEvent ): void	{
			scratching(container.mouseX, container.mouseY);
		}
		
		private function createBitmap(): void	{
			if (bd)	bd.dispose();
			bm = null;
			bd = new BitmapData(image.width, image.height, false, 0xffffff);
			bd.fillRect( new Rectangle(20, 30, 220, 130), 0x000000);
			bm = new Bitmap( bd );
		}
		
		private function onUp( e: MouseEvent ): void	{
			image.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
			image.stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
		}
		
		private function scratching( x:Number, y:Number ): void	{
			maskSpr.graphics.lineStyle(0, 0xff6600, 0, false);
			maskSpr.graphics.beginFill( 0xff6600, alpha);
			maskSpr.graphics.drawCircle(x, y, size);
			maskSpr.graphics.endFill();
			
			checkSpr.graphics.lineStyle(0, 0xff6600, 0, false);
			checkSpr.graphics.beginFill( 0xff6600, alpha);
			checkSpr.graphics.drawCircle(x, y, size);
			checkSpr.graphics.endFill();
			
			checkAreaDraw();
		}
		
		private function checkAreaDraw(): void	{
			bd.draw(checkSpr);
			var temp:Rectangle = bd.getColorBoundsRect(0xffffff, 0x000000, true);
			debugDraw(temp.x, temp.y, temp.width, temp.height);
			
			if (debugSpr.width <= 1 && debugSpr.height <= 1)	{
				dispatchEvent( new Event( Scratch.COMPLETE ) );
			}
		}
		
		private function debugDraw(x:Number, y:Number, width:Number, height:Number): void	{
			debugSpr.graphics.clear();
			debugSpr.graphics.lineStyle(1, 0x000000, 1);
			debugSpr.graphics.beginFill(0x000000, 0.3);
			debugSpr.graphics.drawRect(x, y, width, height);
			debugSpr.graphics.endFill();
		}
		
	}	// class
	
}	// package